head command
head
- output the first part of files
The head
command in Linux is a simple utility used to display the beginning (top) of a file or input. It’s great for quickly checking the start of logs, scripts, or any text file without opening the whole thing.
Usage: head [OPTION]... [FILE]...
OPTION
: Flags which enhances thehead
abilities.FILE
: The file(s) to read (optional; reads from input if omitted).
Print the first 10 lines of each FILE
to standard output. With more than one FILE
, precede each with a header giving the file name. With no FILE
, or when FILE
is -
, read standard input.
Examples
-
Basic Usage
By default,
head
shows the first 10 lines of a file.$ head logfile.txt
- Prints the first 10 lines of
logfile.txt
. - If the file has fewer than 10 lines, it shows the entire file.
- Prints the first 10 lines of
-
Specifying Number of Lines
Use
-n
followed by a number to display a specific number of lines.$ head -n 5 logfile.txt
- Shows the first 5 lines of
logfile.txt
.
Shorthand:
You can omit the
-n
and just use a dash with a number:head -5 logfile.txt
- Same as above.
- Shows the first 5 lines of
-
Working with Multiple Files
Pass multiple files to see the start of each one.
$ head file1.txt file2.txt
- Output includes the first 10 lines of
file1.txt
, thenfile2.txt
, with a header like==> file1.txt <==
separating them.
Single File with Multiple:
To avoid headers for multiple files, use
-q
(quiet):$ head -q -n 5 file1.txt file2.txt
- Shows 5 lines from each file without headers.
- Output includes the first 10 lines of
-
Specifying Bytes Instead of Lines
Use
-c
to display a specific number of bytes instead of lines.$ head -c 20 logfile.txt
- Prints the first 20 bytes of
logfile.txt
(useful for binary files or precise cuts).
- Prints the first 20 bytes of
-
Piping Input
Use
head
with other commands via pipes to process output.$ ls -l | head -n 3
- Shows the first 3 lines of the directory listing.
-
Verbose Output for Multiple Files
Add
-v
to always show file headers, even for a single file.head -v -n 5 logfile.txt
- Output starts with
==> logfile.txt <==
followed by the first 5 lines.
- Output starts with
To get help related to the head
command use --help
option
$ head --help
Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=[-]NUM print the first NUM bytes of each file;
with the leading '-', print all but the last
NUM bytes of each file
-n, --lines=[-]NUM print the first NUM lines instead of the first 10;
with the leading '-', print all but the last
NUM lines of each file
-q, --quiet, --silent never print headers giving file names
-v, --verbose always print headers giving file names
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.